<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
    String path = request.getContextPath();
    if (path.length() > 0) path = path.substring(0, path.length() - 1);
%>
<jsp:include flush="true" page="/view/inc/incTop.jsp"/>
<!-- head-->
<script language="javascript">
    function changeCount(goodId, count) {
        var r1 = /^[0-9]*[1-9][0-9]*$/
        //正整数
        var val = r1.test(count); //str为你要判断的字符 执行返回结果 true 或 false
        if (val == false) {
            alert("数量必须是数字,请重新输入");
        }
        else {
            document.getElementById("indicator1").style.display = "block";
            CartService.changeCount(goodId, count, callback);
        }
    }

    function callback(data) {
        if (data == 'noInventory') {
            alert("库存不足");
        } else if (data == 'tooMuch') {
            alert("单品购买不能超过100件");
        } else if (data == 'error') {
            alert("出现错误");
        }
        document.getElementById("indicator1").style.display = "none";
        document.location.reload(true);
    }


    function delGoodFromCart(goodId) {
        document.getElementById("indicator1").style.display = "block";
        CartService.delGoodFromCart(goodId, callback);
    }

    function clearCart() {

        document.getElementById("indicator1").style.display = "block";
        CartService.clearCart(callback);
    }
</script>

<div class="container well" style="background:#fff;padding-top:0px;">
    <h2>购物车</h2>
    <div class="row" style="margin-left:-19px;margin-right:-19px;">
        <hr style="margin-top:0">
        <s:set var="items" value="#session.cart.items"/>
        <table class="table table-striped table-hover">
            <tr align="center" style="background:#4a9bd6;color: #fff;">
                <td>商品名称</td>
                <td>价格</td>
                <td>数量</td>
                <td>金额</td>
                <td>删除</td>
            </tr>
            <s:iterator value="#items">
                <tr align='center'>
                    <td>
                        <s:property value="value.good.goodName"/>
                    </td>
                    <td>￥
                        <s:property value="value.good.goodPrice"/><br/></td>
                    <td><input type="text" class="textBox"
                               onChange="changeCount(${value.good.goodId},this.value)" value="${value.count}" size="4"/>
                    </td>
                    <td>￥
                        <s:property value="value.count * value.good.goodPrice"/>
                    </td>
                    <td>
                        <a href="#" onClick="delGoodFromCart(<s:property value=" value.good.goodId "/>)"><span
                            class="glyphicon glyphicon-remove"></span></a>
                    </td>
                </tr>
            </s:iterator>
        </table>
        <jsp:include page="/common/page.jsp" flush="true"/>
    </div>
    <div class="row">
        <div class="col-md-3 col-md-offset-9" style="padding-bottom:5px; padding-top:5px;">
            总金额：￥<span><s:property value="#session.cart.totalPrice"/></span>
            <img id="indicator1" src="<%=path %>/img/loading.gif" style="display:none"/>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3 col-md-offset-9">
            <a href="#" onclick="clearCart()" class="btn btn-danger">
                清空
            </a>
            </td>
            <td>
                <a href="<%=path %>/view/default.jsp" class="btn btn-default">
                    继续购物
                </a>
            </td>
            <td>
                <a href="<%=path %>/order/orderConfirm.action" class="btn btn-success">
                    下一步
                </a>
            </td>
        </div>
    </div>
</div>
<script type='text/javascript' src='/dwr/interface/CartService.js'></script>
<!--foot -->
<jsp:include flush="true" page="/view/inc/incFoot.jsp"/>
<!--foot -->	